package com.performax.utils;

import java.io.File;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.UploadPartRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.MultipleFileUpload;
import com.amazonaws.services.s3.transfer.Upload;

public class Oh2gameUtils {
	
	public static String serverOpType = "LIVE"; //DEV:LIVE
	public static HashMap<String, Object>	GLOBAL_SETTING	=	null;
	public static int	SESSION_EXPIRE_TERM_HOUR	=	24;
	
	public static String FORMAT_YYYY_MM_DD_HH_MIN_SS = "yyyy-MM-dd HH:mm:ss";
	
	public static String getAuthKey(String userId, String phone){
		StringBuffer seed	=	new	StringBuffer();
		seed.append(userId).append("+").append(phone).append("+").append(System.currentTimeMillis());
		
		String result	=	"";
		try{
			result	=	HashUtil.getEncSHA256(seed.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return result;
	}
	public static String getAuthNo(){
		Random r	=	new	Random(System.currentTimeMillis());

		String	result	=	String.valueOf(r.nextInt(10000000));
		
		int	leng	=	result.length();
		
		if(leng	<	6){
			int cnt		=	6 - leng;
			for(int i = 0 ; i < cnt ; i++){
				result	=	"0"+result;
			}
		}else if(leng	>	6){
			int cnt		=	leng - 6;
			result	=	result.substring(cnt, result.length());
		}
		
		return result;
	}
	
	public static String genTempPswd(){
		String password = "";
		for(int i = 0; i < 8; i++){
			char lowerStr = (char)(Math.random() * 26 + 97);
			if(i%2 == 0){
				password += (int)(Math.random() * 10);
			}else{
				password += lowerStr;
			}
		}
		 
		//System.out.println(password);
		
		return password;
	}
	
	public static int getAge(String birth){
		Date	now	=	new	Date(System.currentTimeMillis());
		SimpleDateFormat	format	=	new	SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String	current	=	format.format(now);
		
		birth	=	birth.substring(0, 16);
		current	=	current.substring(0,16);
		
		int	birthYear	=	Integer.parseInt(birth.substring(0,4));
		int	currentYear	=	Integer.parseInt(current.substring(0,4));
		int	age	=	(currentYear-birthYear)+1;
		
		/*int birthMonth		=	Integer.parseInt(birth.substring(5,7));
		int	currentMonth	=	Integer.parseInt(current.substring(5,7));
		
		int birthDate		=	Integer.parseInt(birth.substring(8,10));
		int	currentDate		=	Integer.parseInt(current.substring(8,10));
		
		if(birthMonth	<	currentMonth){
			return age-1;
		}else{
			if(birthMonth	==	currentMonth){
				if(birthDate	>	currentDate){
					age--;
				}
			}
		}*/
		return age;
	}
	
	public static long	getCurrentTime(String strDateTime, String dateTimeFormat){
		
		SimpleDateFormat format	=	new	SimpleDateFormat(dateTimeFormat);
		Date	dateTime	=	null;
		try{
			dateTime	=	format.parse(strDateTime);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return dateTime.getTime();
	}
	
	public static List<HashMap<String, String>> transferS3(List<File> files,  String path, String bucketName, String serviceDomain) throws Exception{
		//String bucketName	=	"oh2game";
		List<HashMap<String, String>> transferResult	=	new	ArrayList<HashMap<String, String>> ();
		AWSCredentials	credentials	=	null;
		try{
			credentials	=	new	ProfileCredentialsProvider().getCredentials();
		}catch(Exception e){
			e.printStackTrace();
			throw new	AmazonClientException("cannot load the credentials from the credential profile file.", e);
		}
		TransferManager	tx	=	new	TransferManager(credentials);
		//System.out.println("start transfer to s3");
		//System.out.println("number of files : "+files.size());
		//System.out.println("directory : "+directory.getAbsolutePath());
		//System.out.println("directory exist : "+directory.exists());
		
		//File	f	=	files.get(0);
		//System.out.println("file : "+f.getName());
		//System.out.println("file path : "+f.getAbsolutePath());
		
		for(File file	: files){
			//System.out.println("upload ::::: start");
			String orgName	=	URLDecoder.decode(new String(file.getName().getBytes()), "UTF-8");
			String serverName	=	new StringBuffer().append(System.currentTimeMillis()).append(orgName.substring(orgName.lastIndexOf("."))).toString();
			//Upload	upload	=	tx.upload(bucketName, "boardAttachFile/2015/05/"+file.getName(), file);
			
			String url		=	"https://"+serviceDomain+"/"+bucketName+"/"+path+serverName;
			Upload	upload	=	tx.upload(bucketName, path+serverName, file);
			upload.waitForCompletion();
			//System.out.println("upload ::::: finish");
			HashMap<String, String> rs	=	new	HashMap<String, String>();
			rs.put("orgName", orgName);
			rs.put("serverName", serverName);
			rs.put("url", url);
			
			transferResult.add(rs);
		}
		
		//System.out.println("finish transfer to s3");
		tx.shutdownNow();
		
		return transferResult;
	}
	
	public static String transferS3SingleFile(File file,  String path, String bucketName, String serviceDomain, String serverName) throws Exception{
		//String bucketName	=	"oh2game";
		
		AWSCredentials	credentials	=	null;
		try{
			credentials	=	new	ProfileCredentialsProvider().getCredentials();
		}catch(Exception e){
			e.printStackTrace();
			throw new	AmazonClientException("cannot load the credentials from the credential profile file.", e);
		}
		
		TransferManager	tx	=	new	TransferManager(credentials);
		
		String url		=	"https://"+serviceDomain+"/"+bucketName+"/"+path+serverName;
		
		Upload	upload	=	tx.upload(bucketName, path+serverName, file);
		
		upload.waitForCompletion();
		
		tx.shutdownNow();
		
		return url;
	}
}
